package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayType;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayTypeMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayTypeParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网关类型 服务实现类
 *
 * @author ssy
 * @date 2021/12/04 10:18
 */
@Service
public class BizGatewayTypeServiceImpl extends ServiceImpl<BizGatewayTypeMapper, BizGatewayType> implements BizGatewayTypeService {

    /**
     * 分页查询网关类型
     *
     * @param bizGatewayTypeParam
     * @return
     */
    @Override
    public PageResult<BizGatewayType> page(BizGatewayTypeParam bizGatewayTypeParam) {
        // 构造条件
        LambdaQueryWrapper<BizGatewayType> queryWrapper = new LambdaQueryWrapper<>();
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    /**
     * 添加网关类型
     *
     * @param bizGatewayTypeParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizGatewayTypeParam bizGatewayTypeParam) {
        // 将dto转为实体
        BizGatewayType bizGatewayType = new BizGatewayType();
        BeanUtil.copyProperties(bizGatewayTypeParam, bizGatewayType);
        this.save(bizGatewayType);
    }

    /**
     * 删除网关类型
     *
     * @param bizGatewayTypeParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayTypeParam bizGatewayTypeParam) {
        this.removeById(bizGatewayTypeParam.getId());
    }

    /**
     * 编辑网关类型
     *
     * @param bizGatewayTypeParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizGatewayTypeParam bizGatewayTypeParam) {
        // 根据id查询实体
        BizGatewayType bizGatewayType = this.queryBizGatewayType(bizGatewayTypeParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayTypeParam, bizGatewayType);
        this.updateById(bizGatewayType);
    }

    /**
     * 查看详情网关类型
     *
     * @param bizGatewayTypeParam
     * @return
     */
    @Override
    public BizGatewayType detail(BizGatewayTypeParam bizGatewayTypeParam) {
        return this.queryBizGatewayType(bizGatewayTypeParam);
    }

    /**
     * 计算模型数
     *
     * @param bizGatewayTypeParam
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void countByModel(BizGatewayTypeParam bizGatewayTypeParam) {
        QueryWrapper<BizGatewayType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("model", bizGatewayTypeParam.getModel());
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("型号已存在");
        }
       this.add(bizGatewayTypeParam);
    }


    /**
     * 获取网关类型
     *
     * @author ssy
     * @since 2021/12/04 10:18
     */
    private BizGatewayType queryBizGatewayType(BizGatewayTypeParam bizGatewayTypeParam) {
        return this.getById(bizGatewayTypeParam.getId());
    }
}
